﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Windows.Input;
using System.Windows.Media;
using RaisingForce.Raise.Presentation;
using RaisingForce.Raise.Presentation.Localization;
using RaisingForce.Raise.Presentation.ViewModel;

namespace RaisingForce.RaiseHardwareTest.ViewModel
{
    sealed internal class LanguageViewModel :
        ViewModelBase
    {
        #region Constructors

        private const bool threadSafeFieldInit = true;

        internal LanguageViewModel(
            LocalizationLanguage language,
            IEnumerable<LanguageViewModel> languageCollection = null)
        {
            Contract.Requires(language != null);
            this.language = language;
            this.languageCollection = languageCollection;
            this.selectCommand = new Lazy<ICommand>(
                () => new DelegateCommand(p => this.setSelected(true)),
                isThreadSafe: threadSafeFieldInit);
            this.icon = new Lazy<ImageSource>(
                () => ApplicationHelper.GetFlagIcon(language.CultureName, language.IconPath),
                isThreadSafe: threadSafeFieldInit);
        }

        #endregion

        #region Base Overries

        public override string DisplayName
        {
            get
            {
                return this.language.DisplayName;
            }
            protected set
            {
                // do nothing
            }
        }

        #endregion

        #region Private Declarations

        private readonly IEnumerable<LanguageViewModel> languageCollection;

        private void updateLanguagesInCollection()
        {
            if (languageCollection != null)
            {
                foreach (LanguageViewModel languageViewModel in this.languageCollection)
                {
                    if (!object.ReferenceEquals(languageViewModel, this))
                    {
                        languageViewModel.setSelected(false);
                    }
                }
            }
        }

        [ContractInvariantMethod]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.language != null);
            Contract.Invariant(this.selectCommand != null);
            Contract.Invariant(this.icon != null);
        }

        #endregion

        #region Properties

        private readonly LocalizationLanguage language;

        public LocalizationLanguage Language
        {
            get
            {
                Contract.Ensures(Contract.Result<LocalizationLanguage>() != null);
                return this.language;
            }
        }

        #endregion

        #region IsSelected ViewModel Property

        private bool isSelected;    // = false

        public bool IsSelected
        {
            get { return this.isSelected; }
            set
            {
                // ignore false value!
                if (value == true)
                {
                    this.setSelected(value);
                }
            }
        }

        private void setSelected(bool value)
        {
            if (this.isSelected != value)
            {
                this.isSelected = value;
                this.OnPropertyChanged("IsSelected");
                if (value == true)
                {
                    this.updateLanguagesInCollection();
                    this.OnSelected();
                }
            }
        }

        #endregion

        #region ViewModel Properties

        private readonly Lazy<ICommand> selectCommand;

        public ICommand SelectCommand
        {
            get
            {
                Contract.Ensures(Contract.Result<ICommand>() != null);
                return this.selectCommand.Value;
            }
        }

        private readonly Lazy<ImageSource> icon;

        public ImageSource Icon
        {
            get { return this.icon.Value; }
        }

        #endregion

        #region Selected Event

        public event EventHandler<LocalizationLanguageEventArgs> Selected;

        private void OnSelected()
        {
            this.OnEvent(
                this.Selected,
                new LocalizationLanguageEventArgs(this.language));
        }

        #endregion
    }
}
